package edu.unh.sustain;

import java.util.Hashtable;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class BuildingListActivity extends ListActivity {
	private Hashtable<String, String> buildings;
	
	/*********************************************************************************/
	/********************** DECLARATIONS END *****************************************/
	/*********************************************************************************/
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int count;
		String[] buildingsArray;
		String name;
		Document doc;
		ListView lv;
		
		NodeList nl;
		Node node;
		NamedNodeMap nnp;
		
		// request the document file
	    doc = XMLfunctions.buildingXMLfromWeb();
	    nl = doc.getElementsByTagName("building");
	    count = nl.getLength();
	    
	    // this hash stores the ID info, so on click the building can
	    // be looked up
	    buildings = new Hashtable<String, String>();
	    
	    buildingsArray = new String[count];
	    
	    // loop to grab all the building info
	    for(int i = 0; i < count; i++)
	    {
	    	node = nl.item(i);
	    	nnp = node.getAttributes();
	    	
	    	// change all uppercase names to lowercase with first char capitalized
	    	name = sanitizeText(nnp.getNamedItem("name").getNodeValue().toLowerCase());
	    	buildings.put(name, nnp.getNamedItem("id").getNodeValue());
	    	
	    	buildingsArray[i] = name;
	    }
	    
	    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, buildingsArray));
	    
	    lv = getListView();
	    lv.setTextFilterEnabled(true);
	    
	    // this defines what happens when the item is chosen, it 
	    // will be changed later
	    lv.setOnItemClickListener(new OnItemClickListener() {
	    	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	    		
	    		// what needs to happen here is the app need to get the ID of the building
	    		// that was clicked and send it over to another activity
	    		String key = buildings.get(((TextView) view).getText());
	    		
	    		Intent intent = new Intent(BuildingListActivity.this, UtilMenuActivity.class);
	    		intent.putExtra("building_id", key);
	    		intent.putExtra("building_name", ((TextView) view).getText());
	    		
	    		setResult(RESULT_OK, intent);
	    		finish();
	    	}
	    });
	}
	
	// cleans up the strings in the list
	private String sanitizeText(String s)
	{
		boolean prevWasWhiteSp = true;
	    char[] chars = s.toCharArray();
	    for (int i = 0; i < chars.length; i++) {
	        if (Character.isLetter(chars[i])) {
	            if (prevWasWhiteSp) {
	                chars[i] = Character.toUpperCase(chars[i]);    
	            }
	            prevWasWhiteSp = false;
	        } else {
	            prevWasWhiteSp = Character.isWhitespace(chars[i]);
	        }
	    }
	    return new String(chars);
	}
}
